package org.btik.light.dao;

import org.btik.light.cache.CurrentMemCachePo;
import org.btik.light.cache.LightCache;
import org.btik.light.meta.cache.DevDao;
import org.btik.light.server.platform.common.api.dao.LightDao;
import org.btik.light.meta.DevMo;
import org.btik.light.meta.LightNode;
import org.btik.light.meta.node.SystemNode;
import org.btik.light.tool.type.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.btik.light.meta.LightDevAttrConstant.CAMERA_DEV;

@Service
public class DefaultDao implements LightDao, DevDao {

    @Autowired
    private LightCache lightCache;

    @Override
    public Collection<DevMo> listAtNode(String node) {
        LightNode nodeByLocation = LightServerCache.getNodeByLocation(node);
        if (Objects.isNull(nodeByLocation)) {
            return Collections.emptyList();
        }
        return LightServerCache.getChildren(nodeByLocation);
    }

    @Override
    public void addDev(LightNode lightNode, DevMo moMeta) {
        LightServerCache.addMO(lightNode, moMeta);
    }

    @Override
    public LightNode addNode(LightNode lightNode) {
        return LightServerCache.addNode(lightNode);
    }

    @Override
    public LightNode getThisSystemNode() {
        LightNode systemNode = LightServerCache.getNodeByLocation(LightNode.ROOT);
        if (Objects.isNull(systemNode)) {
            systemNode = SystemNode.getInstance();
            addNode(systemNode);
        }
        return systemNode;
    }

    @Override
    public DevMo getMO(String sn) {
        return LightServerCache.getMO(sn);
    }

    @Override
    public <T extends CurrentMemCachePo<T>> T get(String key, Class<T> type) {
        return lightCache.get(key, type);
    }

    @Override
    public <T extends CurrentMemCachePo<T>> T save(T po) {
        return lightCache.save(po);
    }

    @Override
    public <T extends CurrentMemCachePo<T>> Collection<T> list(Class<T> type) {
        return lightCache.list(type);
    }

    @Override
    public Collection<DevMo> listCameraAtNode(String node) {
        LightNode nodeByLocation = LightServerCache.getNodeByLocation(node);
        if (Objects.isNull(nodeByLocation)) {
            return Collections.emptyList();
        }
        Collection<DevMo> children = LightServerCache.getChildren(nodeByLocation);
        if (CollectionUtil.isEmpty(children)) {
            return Collections.emptyList();
        }
        return children.stream().filter(devMo -> {
            Map<String, String> attr = devMo.getExtendedAttributes();
            if (attr == null) {
                return false;
            }
            return Boolean.parseBoolean(attr.get(CAMERA_DEV));
        }).toList();
    }

    @Override
    public DevMo getDev(String sn) {
        return getMO(sn);
    }

    @Override
    public <T extends CurrentMemCachePo<T>> void delete(String id, Class<T> type) {
        lightCache.remove(id, type);
    }
}
